package com.example.reggie_take_out.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie_take_out.commen.R;
import com.example.reggie_take_out.dto.SetmealDto;
import com.example.reggie_take_out.entry.Category;
import com.example.reggie_take_out.entry.Setmeal;
import com.example.reggie_take_out.service.CategoryService;
import com.example.reggie_take_out.service.SetmealDishService;
import com.example.reggie_take_out.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;





    /**
     * 套餐分页查询操作
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize ,String name){
        //创建分页构造器
            Page<Setmeal> mealPageInfo = new Page<>();
            Page<SetmealDto> mealDtoPageInfo = new Page<>();
        //创建条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null ,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(mealPageInfo,queryWrapper);
        BeanUtils.copyProperties(mealPageInfo,mealDtoPageInfo,"setmealDishes");

        List<SetmealDto> list=mealPageInfo.getRecords().stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);

            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());

        mealDtoPageInfo.setRecords(list);
        return R.success(mealDtoPageInfo);
    }



    /**
     * 添加套餐操作
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmeal" , allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加套餐成功");
    }

    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable int status,Long []ids){
        for (int i = 0; i < ids.length; i++) {
            Setmeal setmeal = setmealService.getById(ids[i]);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("状态修改成功");
    }

    /**
     * 删除操作
     * @param ids
     * @return
     */

    @DeleteMapping
    @CacheEvict(value = "setmeal" , allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){

            setmealService.deleteWithDish(ids);

        return R.success("删除成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmeal" , key = "#setmeal.categoryId+'_'+#setmeal.status" , unless = "#result == null")
    public R<List<Setmeal>> list( Setmeal setmeal){
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(setmeal.getCategoryId()!= null,Setmeal::getCategoryId,setmeal.getCategoryId());
            queryWrapper.eq(setmeal.getStatus()!= null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);

    }



}
